<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/10
 * Time: 下午1:53
 * 上门车配件
 */

class OdsPartController extends BaseController{
    /*
     * 修改配件状态
     * */
    public function change_part_status(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'car_id' => 'required',
                'status' => 'required',
                'part_id' => 'required',
                'date' => 'required',
                'engineer_id' => 'required',
                'main_engineer_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $shop_id = Input::get('shop_id');
            $car_id = Input::get('car_id');
            $date = Input::get('date');
            $status = Input::get('status');
            $part_id = Input::get('part_id');
            $engineer_id = Input::get('engineer_id');
            $main_engineer_id = Input::get('main_engineer_id');            


            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                try{
                    $orders = Orders::where('shop_id',$shop_id)
                        ->where('car_id',$car_id)
                        ->where('book_time',$date)
                        ->where('status',Config::get('order.service_confirm_order_status'))->update(array("engineer_id"=>$main_engineer_id));

                    $id = json_decode($part_id);
                    $count = MaintenanceCarParts::where('shop_id',$shop_id)
                        ->where('car_id',$car_id)
                        ->where('date',$date)
                        ->whereIn('id',$id)
                        ->update(
                            array(
                                'status' => $status,
                                'engineer_id' => $engineer_id
                            )
                        );


                } catch (Exception $e) {
                    Log::error("error", array("system" => "ods" ,"event" => "change_part_status", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("ods_error.create_maintenance_car_parts_error_12011");
                    break;
                }
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "change_part_status", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => $count
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "change_part_status", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 获取上门车配件列表
     * */
    public function get_part_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'car_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $shop_id = Input::get('shop_id');
            $car_id = Input::get('car_id');
            $date = Input::get('date')?Input::get('date'):date('Y-m-d',time());
            $status = Input::get('status')?Input::get('status'):1;

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                $parts = MaintenanceCarParts::where('shop_id',$shop_id)
                    ->where('car_id',$car_id)
                    ->where('date',$date)
                    ->where('status',$status)
                    ->get()
                    ->toArray();
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "get_part_list", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => count($parts),
                    'date' => $date,
                    'parts' => $parts
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "get_part_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 创建上门车订单所用配件列表
     * */
    public function create_maintenance_car_parts(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'shop_id' => 'required',
                'car_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $shop_id = Input::get('shop_id');
            $car_id = Input::get('car_id');
            $date = Input::get('date')?Input::get('date'):date('Y-m-d',time());
//            $status = Input::get('status')?Input::get('status'):-1;

            //验证token
//            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
//            if(!is_null($car_info)){
                try{
                    //清理记录表
                    MaintenanceCarParts::where('shop_id',$shop_id)
                        ->where('car_id',$car_id)
                        ->where('date',$date)
                        ->update(
                            array(
                                'status' => 0,
                            )
                        );

                    //获取上门订单
                    $orders = Orders::where('shop_id',$shop_id)
                        ->where('car_id',$car_id)
                        ->where('book_time',$date)
                        ->where('status',Config::get('order.service_confirm_order_status'))
                        ->select(array('order_id'))
                        ->get()
                        ->toArray();
                    //获取配件清单
                    foreach($orders as $_order){
                        $order_product=OrderProduct::where("order_id",$_order['order_id'])->where("status",1)->get()->toArray();
                        foreach ($order_product as $_op) {
                            switch ($_op['product_type']) {
//                            case 'promotion':
//                                $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
//                                $_op->package=$opp;
//                                $promotion_procut[]=$_op;
//                                break;
                                case 'base':
                                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                                        ->leftJoin('supply_item','order_product_base_item.item_id','=','supply_item.id')
                                        ->leftJoin('supply_item_non_standard','order_product_base_item.item_id','=','supply_item_non_standard.id')
                                        ->where('order_product_base_item.type',1)
                                        ->select('order_product_base_item.*','supply_item.subject as sis','supply_item_non_standard.subject as sinss')
                                        ->get()
                                        ->toArray();
                                    //记录记录表
                                    foreach($opp as $_opp){
                                        $maintenance_car_part = new MaintenanceCarParts();
                                        $maintenance_car_part->date = $date;
                                        $maintenance_car_part->shop_id = $shop_id;
                                        $maintenance_car_part->car_id = $car_id;
                                        $maintenance_car_part->order_id = $_opp['order_id'];
                                        $maintenance_car_part->order_product_package_id = $_opp['order_product_package_id'];;
                                        $maintenance_car_part->item_id = $_opp['item_id'];;
                                        $maintenance_car_part->item_name = $_opp['item_name'];;
                                        $maintenance_car_part->amount = $_opp['amount'];;
                                        $maintenance_car_part->num = $_opp['num'];;
                                        $maintenance_car_part->unit = $_opp['unit'];;
                                        $maintenance_car_part->status = 1;
                                        $maintenance_car_part->created_at = Common::num2DateTime(time());
                                        $maintenance_car_part->updated_at = Common::num2DateTime(time());
                                        $maintenance_car_part->save();
                                    }
                                    break;
//                            case 'non_standard':
//                                $falg=true;
//                                $opn=OrderProductNonStandard::where("order_product_id",$_op->id)
//                                    ->leftJoin('supply_item','order_product_non_standard.item_id','=','supply_item.id')
//                                    ->leftJoin('supply_item_non_standard','order_product_non_standard.item_id','=','supply_item_non_standard.id')
//                                    ->select('order_product_non_standard.*','supply_item.subject as sis','supply_item_non_standard.subject as sinss')
//                                    ->get()
//                                    ->toArray();
//                                var_dump()
//                                // $opn=DB::select("SELECT opbi.*,moo.status as moo_status  from order_product_non_standard  opbi
//                                //     left join   (select * from maintenance_out_order where product_type='non_standard' and status in(0,1,2)) as moo on moo.product_item_id=opbi.id
//                                //     where opbi.order_product_id=?",array($_op->id));
//                                foreach ($opn as $key=>$value) {
//                                    //判断产品项是非标还是标准配件
//                                    if($value->service_type==1){
//                                        if($value->type==0){
//                                            unset($opn[$key]['sinss']);
//                                            $opn[$key]['subject']=$opn[$key]['sis'];
//                                            unset($opn[$key]['sis']);
//                                        }else{
//                                            unset($opn[$key]['sis']);
//                                            $opn[$key]['subject']=$opn[$key]['sinss'];
//                                            unset($opn[$key]['sinss']);
//                                        }
//
//                                    }else{
//                                        unset($opn[$key]['sinss']);
//                                        unset($opn[$key]['sis']);
//                                    }
//                                }
//                                $_op->item=$opn;
//                                $_op->moo_status=$falg;
//                                $_op->col=sizeof($opn);
//                                $non_standard[]=$_op;
//                                break;
                            }
                        }
                    }
                } catch (Exception $e) {
                    Log::error("error", array("system" => "ods" ,"event" => "create_maintenance_car_parts", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("ods_error.create_maintenance_car_parts_error_12011");
                    break;
                }
//            }else{
//                Log::error("error", array("system" => "ods" ,"event" => "get_order_list", 'input' =>Input::all()));
//                $response = Lang::get("ods_error.token_check_error_12005");
//                break;
//            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => count($orders),
                    'date' => $date,
                    'orders' => $orders
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "get_order_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}